<?php
//       PHPcongrats - Holiday and Birthday Mailer
//     
//    Copyright (C) 2011  Matthias Nass
//
//    This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License along with this program; if not, see <http://www.gnu.org/licenses/>.

session_start();

if ($_SESSION['loggedin'] == true){

     $_SESSION['savedFirstname'] = $_POST['firstname'];
     $_SESSION['savedLastname']  = $_POST['lastname'];
     $_SESSION['savedTitle']  = $_POST['title'];
     $_SESSION['savedHometown'] =$_POST['hometown'];
     $_SESSION['savedGender'] = $_POST['gender'];
     $_SESSION['savedEmail'] = $_POST['email'];
     $_SESSION['savedDay'] = $_POST['day'];
     $_SESSION['savedMonth'] = $_POST['month'];
     $_SESSION['savedYear'] = $_POST['year'];
     $_SESSION['savedBirthgrats'] = $_POST['birthgrats'];
     $_SESSION['savedLang'] = $_POST['lang'];
     
     // settype desaster - ich brauche Hilfe zum sicherstellen als integer
     $year = $_POST['year'];
     $month = $_POST['month'];
     $day = $_POST['day'];
     $year = (int) $year;
     $month = (int )$month;
     $day = (int) $day;
     
     include 'mailcheck.php';
     if(check_email($_POST['email']) == false){
     		$_SESSION['message'] = "E-Mail Adresse ist nicht g&uuml;ltig!";
     		$location_to_jump = "Location: http:./personadd.php";
     		header( $location_to_jump );     
     }elseif (($_POST['firstname']=='')){
     		$_SESSION['message'] = "Vorname ist leer!";
     		$location_to_jump = "Location: http:./personadd.php";
     		header( $location_to_jump );     
     }elseif (($_POST['lastname']=='')){
     		$_SESSION['message'] = "Nachname ist leer!";
     		$location_to_jump = "Location: http:./personadd.php";
     		header( $location_to_jump );     
     }elseif (($_POST['hometown']=='')){
     		$_SESSION['message'] = "Heimatstadt ist leer!";
     		$location_to_jump = "Location: http:./personadd.php";
     		header( $location_to_jump );     
     }else{
             if ($_POST['birthgrats'] == 'on'){
                  $birthgrats=1;
             }else{
                  $birthgrats=0;
             }
             include "./DBconnect.php";
             $query = "INSERT INTO persons VALUES(null, 
             '".mysql_real_escape_string($_POST['firstname'])."',
             '".mysql_real_escape_string($_POST['lastname'])."',
             '".mysql_real_escape_string($_POST['title'])."',
             '".mysql_real_escape_string($_POST['hometown'])."',
             '".mysql_real_escape_string($_POST['gender'])."',
             '".mysql_real_escape_string($_POST['email'])."',
             '".strval ( $year )."-".strval ( $month )."-".strval ( $day )."'
             ,".$birthgrats.",
             '".mysql_real_escape_string($_POST['lang'])."', CURRENT_TIMESTAMP 
     					, CURRENT_TIMESTAMP);";   
             $result = mysql_db_query($database, $query, $link);
             mysql_close($link);
            if($result == false){
     		$_SESSION['message'] = $query;
     		$location_to_jump = "Location: http:./personadd.php";
     		header( $location_to_jump );          
             }else {
               $_SESSION['savedFirstname'] = "";
               $_SESSION['savedLastname']  = "";
               $_SESSION['savedTitle']  = "";
               $_SESSION['savedHometown'] = "";
               $_SESSION['savedGender'] = "";
               $_SESSION['savedEmail'] = "";
               $_SESSION['savedDay'] = "";
               $_SESSION['savedMonth'] = "";
               $_SESSION['savedYear'] = "";
               $_SESSION['savedBirthgrats'] = "";
               $_SESSION['savedLang'] = "";
             $_SESSION['message'] = "Person gespeichert";
     		$location_to_jump = "Location: http:./persons.php";
     		header( $location_to_jump );                       
             }    			
     }
} else{
	$_SESSION['last_location'] = "./personadd.php";
	header( 'Location: http:./login.php' );
}
?>